package uit.server;

import java.util.Iterator;

import uit.server.arcims.ArcIMSServer;

import com.esri.aims.mtier.io.ConnectionProxy;
import com.esri.aims.mtier.model.map.Layers;
import com.esri.aims.mtier.model.map.Map;
import com.esri.aims.mtier.model.map.layer.FeatureLayer;
import com.esri.aims.mtier.model.map.layer.Layer;
import com.esri.aims.mtier.model.map.layer.query.Filter;

public class FeatrureService {
	
	public static void main(String[] args) {
		ArcIMSServer server= new ArcIMSServer();
		String servicename = "UPIS3_DATA";
		String hostname = "192.168.0.20";
		int port = 5300;
		server.setDisplayMessages(true);
		Map map = new Map();
		try {
			ConnectionProxy connectionProxy = new ConnectionProxy();
			
			connectionProxy.setConnectionType(ConnectionProxy.TCP);
			connectionProxy.setHost(hostname);
			connectionProxy.setPort(port);
			connectionProxy.setDisplayMessages(true);
			connectionProxy.setService(servicename);
			System.out.println(connectionProxy.ping()  + "," +ConnectionProxy.PING_OK) ;
			
			try {
				map.initMap(connectionProxy, 96, true, false, true, true);
			} catch (Exception e) {
			}
			
			
			
			FeatureLayer featureLayer = null;
			Layers layers = map.getLayers();
			System.out.println(layers.getCount() + " layer cout ");
			featureLayer= getFeatureLayer(layers, 3);
//			
			Filter filter = new Filter();
			filter.clearSubFields();
			filter.setBoundingEnvelope(true);
			filter.setGlobalEnvelope(true);
			filter.setWhereExpression("OWN_GID=10");
			filter.setSkipFeatures(true);
			filter.setCheckesc(true);
			featureLayer.setFilterObject(filter);
			System.out.println(map.getArcXML());
			System.out.println(map.sendArcXML(map.getArcXML(),Map.GET_FEATURE));
//			int count = featureLayer.getRecordset().getCount();
//			 long featurecount=featureLayer.getRecordset().getFeaturesCount();
//System.out.println(count + " : " + featurecount);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static FeatureLayer getFeatureLayer(Layers layers , int id){
		FeatureLayer featureLayer = null;
		for (Iterator iter = layers.getLayersCollection().iterator(); iter.hasNext();) {
			Layer layer = (Layer) iter.next();
			System.out.println(layer.getType() + " " + layer.getType().equals("Feature"));
			if(layer.getType().equals("Feature")){
				featureLayer = (FeatureLayer) layer;
				System.out.println(featureLayer.getID() + " 3" );
				if(featureLayer.getID().equals(id+"")){
					System.out.println(featureLayer.getID() + " true " );
					featureLayer = (FeatureLayer) layer;
					break;
				}
			}
		}
		return featureLayer;
	}

}
